package org.rain.core.querygen.support;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.rain.common.util.Strs;
import org.rain.core.querygen.AbsQueryCondition;
import org.rain.core.querygen.token.Token;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * created by yangtong on 2025/4/6 下午10:36
 * <p>
 * 小于等于 age_le=23 -> age <= 21
 */
@Component
public class LtQueryCondition extends AbsQueryCondition implements Ordered {

    private final static String SUFFIX = "_lt";

    @Override
    public Boolean isMatch(Token token) {
        return Strs.isNotEmpty(token.getValue()) && token.getKey().endsWith(SUFFIX);
    }

    @Override
    public <T> QueryWrapper<T> genQueryWrapper(QueryWrapper<T> queryWrapper, Token token) {
        String realKey = token.getKey().substring(0, token.getKey().length() - SUFFIX.length());
        token.setKey(realKey);
        return positiveOrNegate(queryWrapper, token);
    }

    @Override
    protected <T> QueryWrapper<T> positive(QueryWrapper<T> queryWrapper, Token token) {

        return queryWrapper.lt(Strs.toUnderline(token.getKey()), token.getValue());
    }

    @Override
    protected <T> QueryWrapper<T> negate(QueryWrapper<T> queryWrapper, Token token) {
        return queryWrapper.not(w -> positive(w, token));
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE + 1;
    }
}
